env_logger
Implements a logger that can be configured via environment variables.
Usage
In libraries
env_logger
makes sense when used in executables (binary projects). Libraries should use the log
crate instead.
In executables
It must be added along with log
to the project dependencies:
[]
= "0.4.0"
= "0.5.13"
env_logger
must be initialized as early as possible in the project. After it's initialized, you can use the log
macros to do actual logging.
extern crate log;
extern crate env_logger;
Then when running the executable, specify a value for the RUST_LOG
environment variable that corresponds with the log messages you want to show.
In tests
Tests can use the env_logger
crate to see log messages generated during that test:
[]
= "0.4.0"
[]
= "0.5.13"
extern crate log;
Assuming the module under test is called my_lib
, running the tests with the
RUST_LOG
filtering to info messages from this module looks like:
; ; ;
Note that env_logger::try_init()
needs to be called in each test in which you
want to enable logging. Additionally, the default behavior of tests to
run in parallel means that logging output may be interleaved with test output.
Either run tests in a single thread by specifying RUST_TEST_THREADS=1
or by
running one test by specifying its name as an argument to the test binaries as
directed by the cargo test
help docs:
; ; ;
Configuring log target
By default, env_logger
logs to stderr. If you want to log to stdout instead,
you can use the Builder
to change the log target:
use env;
use ;
let mut builder = new;
builder.target;
if var.is_ok
builder.init;